package org.rbac.web.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.rbac.content.CacheContent;
import org.rbac.content.configure.RbacConfigure;
import org.rbac.content.define.ContentDefine;
import org.rbac.content.define.RbacKeyDefine.keys;
import org.rbac.util.SpringUtil;

/**
 * 
*  <pre>    
* 类名称：RbacInitalListener 
* 类描述：   容器数据初始化
* 创建人：陈国祥   (kingschan)
* 创建时间：2015-7-6 上午8:59:06   
* 修改人：Administrator   
* 修改时间：2015-7-6 上午8:59:06   
* 修改备注：   
* @version V1.0
* </pre>
 */
@WebListener
public class RbacInitalListener implements ServletContextListener {
	protected final Log logger = LogFactory.getLog(getClass());

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
	    CacheContent.getInstance().shutdown();
	}
	
	@Override
	public void contextInitialized(ServletContextEvent sce) {	
	    String web_inf=sce.getServletContext().getRealPath("WEB-INF");
        
        RbacConfigure configure = RbacConfigure.getInstance();
        configure.set(keys.WEB_INF, sce.getServletContext().getRealPath("WEB-INF"));
        configure.set(keys.CACHE_FOLDER, web_inf.concat("\\cache\\"));
        configure.set(keys.SQL_FOLDER, web_inf.concat("\\sql\\"));        
	    CacheContent.getInstance();
		try {
	        ContentDefine content =(ContentDefine) SpringUtil.getBeanByName("RbacApplicationContent");
	        content.setApplicationMapping();
	        content.loadDictionary(sce.getServletContext());
//	        application.setAttribute(ContentDefine.Application_web_root, application.getContextPath() + "/"); // 跟路径
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
}
